<?php
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Tools_Uploader');
class Oa_Attendance_UploadProofController extends Oa_Attendance_AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $user_id = $_SESSION['user_id'];
        $record_id = $params['id'];
        $burl = $_SERVER['HTTP_REFERER'];
        if(empty($burl) || $burl == 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']) {
            $burl = '/';
        }
        if(!$this->check_consistence($user_id,$record_id)){
            $this->response->redirect('/',false,false);
        }
        if($this->is_post_request()) {
            $upDir = 'upload/attendance';
            $uploader = new Tools_Uploader('upfile');
            $uploader->setValidExt(array(
                    '.jpg',
                    '.gif'
            ));
            $uploader->setUpDir(CODE_PATH . $upDir);
            if(!$uploader->upload()) {
                exit($uploader->getErrorMsg());
            }else {
                $img = $uploader->getFileName();
                apf_require_class('Bll_Attendance_Apply');
                $bll_apply = new Bll_Attendance_Apply();
                $bll_apply->upload_img($record_id,$img);
                echo "<script type='text/javascript'>alert('图片上传成功！')</script>";
                $this->request->set_attribute('burl',$burl);
            }
        }
        return 'Oa_Attendance_UploadProof';
    }

    private function check_consistence ($user_id,$record_id) {
        apf_require_class('Bll_Attendance_LeaveRecord');
        $bll_record = new Bll_Attendance_LeaveRecord;
        $row = $bll_record->get_leave_by_id($record_id);
        return $user_id == $row['user_id'];
    }
}